#include "poweroffloading.h"
#include "ui_poweroffloading.h"

PowerOffLoading::PowerOffLoading(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PowerOffLoading)
{
    ui->setupUi(this);

    if (windowWidth >= 1280) {
        this->setGeometry(0,0,1280,800);
    }
    setAttribute(Qt::WA_TranslucentBackground, true);
    this->setWindowFlags(this->windowFlags() | Qt::SubWindow);
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
    shadow->setColor(QColor(0,0,0,150));
    shadow->setOffset(1, 1);
    shadow->setBlurRadius(16);
    ui->widget->setGraphicsEffect(shadow);
    QTimer * timer = new QTimer(this);
    timer->setInterval(20);
    mPix.load(":/resources/loading.png");
    connect(timer, &QTimer::timeout, this, &PowerOffLoading::rotateImage);

    timer->start();
}

PowerOffLoading::~PowerOffLoading()
{
    delete ui;
}

void PowerOffLoading::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(0, 0, 0, 200));
}

void PowerOffLoading::rotateImage()
{
    static int angle = 0;

    angle += 10;

    angle %= 360;

    QMatrix matrix;

    matrix.rotate(angle);

    QPixmap tmp(80,80);

    tmp.fill(Qt::transparent);

    QPainter painter(&tmp);

    painter.setRenderHint(QPainter::SmoothPixmapTransform);

    painter.translate(40,40);

    painter.rotate(angle);

    painter.translate(-40,-40);

    painter.drawPixmap(0,0,80,80, mPix);

    ui->icon->setPixmap(tmp);
}
